package com.hclc.impl;

import com.hclc.api.PStatisticsRedisCacheService;
import com.hclc.api.redis.IRedisService;
import com.hclc.util.json.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * @ClassName PStatisticsRedisCacheImpl
 * @Author: yurj
 * @Mail：1638234804@qq.com
 * @Date: Create in 10:02 2020/9/6
 * @version: 1.0
 */
@Service
@Slf4j
public class PStatisticsRedisCacheServiceImpl implements PStatisticsRedisCacheService {
    /**
     * 统计默认过期时间
     */
    private static final long DEFAULT_STATISTICS_EXPIRATION = 15L;

    @Autowired
    private IRedisService redisService;


    /**
     * 获取缓存统计数据(默认过期时间)
     *
     * @param refresh
     * @param redisKey
     * @param supplier
     * @param dtoClazz
     * @return T
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/9/6 13:48
     */
    @Override
    public <T> T getStatisticsData(boolean refresh, String redisKey, Supplier supplier, Class<T> dtoClazz) {
        return getStatisticsData(refresh, redisKey, DEFAULT_STATISTICS_EXPIRATION, supplier, dtoClazz);
    }

    /**
     * 获取缓存统计数据(自定义过期时间)
     *
     * @param refresh
     * @param redisKey
     * @param expiration
     * @param supplier
     * @param dtoClazz
     * @return T
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/9/6 13:48
     */
    @Override
    public <T> T getStatisticsData(boolean refresh, String redisKey, long expiration, Supplier supplier, Class<T> dtoClazz) {
        // 如果刷新，直接重新计算
        if (refresh) {
            return initializationData(redisKey, expiration, supplier, dtoClazz);
        }
        // 获取缓存数据
        if (redisService.exists(redisKey)) {
            return JsonUtil.toObject(redisService.get(redisKey), dtoClazz);
        }
        // 如果没有数据,执行初始化
        return initializationData(redisKey, expiration, supplier, dtoClazz);
    }

    /**
     * 初始化数据
     *
     * @param redisKey
     * @param expiration
     * @param supplier
     * @param dtoClazz
     * @return T
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/9/6 13:47
     */
    private <T> T initializationData(String redisKey, long expiration, Supplier supplier, Class<T> dtoClazz) {
        String statisticsData = JsonUtil.toJson(supplier.get());
        refreshCacheData(redisKey, expiration, statisticsData);
        return JsonUtil.toObject(statisticsData, dtoClazz);
    }

    /**
     * 更新缓存数据
     *
     * @param
     * @return void
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/9/6 10:31
     */
    private void refreshCacheData(String redisKey, long expiration, String statisticsData) {
        // 更新缓存数据
        redisService.set(redisKey, statisticsData, expiration, TimeUnit.MINUTES);
    }
}
